﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class FlowerCtrl : ItemCtrl
{
    public Animator am;
    public Animator shadowAm;
    public float sec;
    public Coroutine ct;
    public static int totalNum;
    public static int curActiveNum;
    public static bool isDone;
    public void Start()
    {
        am = GetComponent<Animator>();
        shadowAm = transform.GetChild(0).GetComponent<Animator>();
        am.enabled = false;
        shadowAm.enabled = false;
        gameObject.SetActive(false);
    }
    public override void Check(float force)
    {
        //base.Check(force);
        float minD = 999;
        Vector3 touch = Vector3.zero;
        for (int i = 0; i < touches.Count; i++)
        {
            float d = Vector3.Distance(transform.position, touches[i]);
            if (d < minD)
            {
                minD = d;
                touch = touches[i];
            }
        }
        if (minD < 1.2f)
        {
            //Vector3 dir = (transform.position - touch).normalized;
            //float an = Vector3.Angle(dir, Vector3.up);
            //an = an > 90 ? an - 90 : an;
            //float d = Mathf.Lerp(3f, 50f, an / 90) * (1 - minD / 2);
            //float d = 25f * (1 - minD / 2);
            Do();
        }
    }

    public void Do()
    {
        if (ct == null)
        {
            am.runtimeAnimatorController = Resources.Load<RuntimeAnimatorController>("花开" + Random.Range(1, 4));
            shadowAm.runtimeAnimatorController = am.runtimeAnimatorController;
            shadowAm.enabled = true;
            am.enabled = true;
            gameObject.SetActive(true);
            ct = Utl.instance.WaitForSec(Random.Range(1.6f, 4.5f), () =>
               {
                   am.SetTrigger("qiong");
                   shadowAm.SetTrigger("qiong");
                   Utl.instance.WaitForSec(sec, () =>
                  {
                      am.enabled = false;
                      shadowAm.enabled = false;
                      gameObject.SetActive(false);
                      ct = null;
                  });

               });
        }
    }
}
